﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace KonachanChanger
{
    /// <summary>
    /// This class has various extension methods that we use.
    /// </summary>
    public static class ExtensionMethods
    {
        /// <summary>
        /// Allows us to easily build queries with a UriBuilder.
        /// </summary>
        /// <param name="builder">The builder we're extending.</param>
        /// <param name="Query">The query string (x=y)</param>
        public static void AppendQuery(this UriBuilder builder, string Query)
        {
            if (builder.Query != null && builder.Query.Length > 1)
                builder.Query = builder.Query.Substring(1) + "&" + Query;
            else
                builder.Query = Query;
        }

        /// <summary>
        /// Gives a Human Readable string (ie: 1 hour 5 minutes) from a TimeSpan
        /// </summary>
        /// <param name="span">The extended TimeSpan</param>
        /// <returns>A human readable string.</returns>
        public static string ToReadableString(this TimeSpan span)
        {
            string formatted = string.Format("{0}{1}{2}{3}",
                span.Days > 0 ? string.Format("{0:0} days, ", span.Days) : string.Empty,
                span.Hours > 0 ? string.Format("{0:0} hours, ", span.Hours) : string.Empty,
                span.Minutes > 0 ? string.Format("{0:0} minutes, ", span.Minutes) : string.Empty,
                span.Seconds > 0 ? string.Format("{0:0} seconds", span.Seconds) : string.Empty);

            if (formatted.EndsWith(", ")) formatted = formatted.Substring(0, formatted.Length - 2);

            return formatted;
        }

    }
}
